//
//  SGHomeTableViewCell.swift
//  songguo
//
//  Created by 潘传洲 on 16/6/4.
//  Copyright © 2016年 pcz. All rights reserved.
//

import UIKit
import SDWebImage

class SGHomeTableViewCell: UITableViewCell {

    @IBOutlet weak var cityView: UIView!
    @IBOutlet weak var bgImage: UIImageView!
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var address: UILabel!
    @IBOutlet weak var city: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var flag: UITextField!
    
    
    
    var item : SGHomeListModel?{
        didSet{
            dealData()
        }
    }

    
    override func awakeFromNib() {
        super.awakeFromNib()
        flag.borderStyle = .RoundedRect
    }

    func dealData() {
        let item = self.item!
        
        if item.type == .Activity {//activity
            
            let activity = item.activity
            let format = NSDateFormatter()
            format.dateFormat = "M.d"
            let stStr = format.stringFromDate(NSDate(timeIntervalSince1970: (activity!.st! / 1000)))
            let etStr = format.stringFromDate(NSDate(timeIntervalSince1970: (activity!.et! / 1000)))
            time.text = stStr + "-" + etStr
            address.text = activity!.address!
            city.text = activity?.geo?.city?.name!.stringByReplacingOccurrencesOfString("市", withString: "")
            name.text = activity!.name
            flag.hidden = false
            cityView.hidden = false
            address.hidden = false
            bgImage.sd_setImageWithURL(NSURL(string: (activity!.coverUrl)!), placeholderImage: UIImage(named: "home_cell_placeholderImage"))
            
        }else{//article
            let article = item.article
            flag.hidden = true
            cityView.hidden = true
            address.hidden = true
            name.text = article!.title
            time.text = "by " + (article!.user?.nick)!
            bgImage.sd_setImageWithURL(NSURL(string: (article!.coverUrl)!), placeholderImage: UIImage(named: "home_cell_placeholderImage"))
        }
    
    }
    
    
    override func setHighlighted(highlighted: Bool, animated: Bool) {
        
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        
    }
    
    
}
